﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using compshop.Modules.Producers.Presenter;
using compshop.Infrastructure.CompShopDataService;

namespace compshop.Modules.Producers.Views
{
    /// <summary>
    /// Interaction logic for ProducersView.xaml
    /// </summary>
    public partial class ProducersView : UserControl, IViewProducer
    {
        private Label selectedLabel;

        public ProducersView()
        {
            InitializeComponent();
        }

        public ProducersView(ProducersPresenter presenter) 
            : this()
        {
            Presenter = presenter;
            Presenter.View = this;
        }

        public ProducersPresenter Presenter
        {
            get { return DataContext as ProducersPresenter; }
            set
            {
                DataContext = value;

                Label lbl = new Label(); ;
                lbl = new Label();
                lbl.Content = "All";
                lbl.Tag = 0;
                lbl.Margin = new Thickness(2, 5, 2, 5);
                lbl.Foreground = Brushes.Lavender;
                selectedLabel = lbl;
                lbl.Background = new SolidColorBrush(Colors.LightGray);
                lbl.MouseLeave += new MouseEventHandler(lbl_MouseLeave);
                lbl.MouseEnter += new MouseEventHandler(lbl_MouseEnter);
                lbl.MouseLeftButtonDown += new MouseButtonEventHandler(lbl_MouseLeftButtonDown);
                wrpPanel.Children.Add(lbl);

                foreach (Producer prd in Presenter.ProducerList)
                {
                    lbl = new Label();
                    lbl.Content = prd.Name;
                    lbl.Tag = prd.ProducerID;
                    lbl.Margin = new Thickness(2, 5, 2, 5);
                    lbl.Background = new SolidColorBrush(Colors.LightGray);
                    lbl.MouseLeave+=new MouseEventHandler(lbl_MouseLeave);
                    lbl.MouseEnter+=new MouseEventHandler(lbl_MouseEnter);
                    lbl.MouseLeftButtonDown+=new MouseButtonEventHandler(lbl_MouseLeftButtonDown);
                    wrpPanel.Children.Add(lbl);
                }
            }
        }

        private void lbl_MouseLeave(object sender, MouseEventArgs e)
        {
            ((Label)e.Source).Background = Brushes.LightGray;
        }

        private void lbl_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Label)e.Source).Background = Brushes.Gray;
        }

        private void lbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (selectedLabel != null)
                selectedLabel.Foreground = Brushes.Black;

            selectedLabel=((Label)e.Source);
            selectedLabel.Foreground = Brushes.Lavender;

            Presenter.ProducerSelectedPublisher(Convert.ToInt32(selectedLabel.Tag));
        }


    }
}
